package com.zhu.day.util;


import com.alibaba.fastjson.JSON;
import com.zhu.day.model.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
public class DanceKingRankUtil {

    public static final String FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
    public static LocalDateTime getDateTimeNow( String testSysTime) {
        try {
            return "0".equals(testSysTime)|| StringUtils.isEmpty(testSysTime)?
                    LocalDateTime.now(): LocalDateTime.parse(testSysTime);
        } catch (Exception e) {
            return LocalDateTime.now();
        }
    }

    public static LocalDateTime date2LocalTime(String date) {
        if (StringUtils.isBlank(date)){
            return LocalDateTime.now();
        }
        LocalDateTime startDateTime = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(FORMAT_YMDHMS));
        return startDateTime;
    }

    public static List<Stage> getStageList() {

        return new ArrayList<>();
    }


    public static Map<Integer, Stage> getStageMap(List<Stage> stages) {
        Map<Integer, Stage> stageMap = stages.stream().collect(
                Collectors.toMap(Stage::getStageNo, (p) -> p));
        return stageMap;
    }

    /**
     *
     * @param stages
     * @param time
     * @return
     */
    public static Optional<Stage> getCurrentStage(List<Stage> stages, LocalDateTime time){
        if(!CollectionUtils.isEmpty(stages)){
            for (Stage stage : stages) {
                boolean result = (time.isAfter(stage.getStageStart()) || time.isEqual(stage.getStageStart())) &&
                        time.isBefore(stage.getStageEnd());
                if (result) {
                    log.info("DanceKingRankUtil.getCurrentStage.stage:{}", JSON.toJSONString(stage));
                    return Optional.ofNullable(stage);
                }
            }
        }
        return Optional.empty();
    }

    //赛果配置
    public static Optional<ResultsInfo> getResultsInfo(){
        String json = "{\n" +
                "     \"startTime\":\"2021-04-29 00:00:00\",\n" +
                "     \"endTime\":\"2021-04-29 02:00:00\",\n" +
                "     \"rank\":8,\n" +
                "     \"showStage\":5,\n" +
                "     \"track\":1000,\n" +
                "     }";
        return Optional.ofNullable(JSON.parseObject(json,ResultsInfo.class));
    }
    //复活赛
    public static Optional<RevivalInfo> getRevivalInfo(){
        String json = "{\n" +
                "     \"startTime\":\"2021-04-28 00:00:00\",\n" +
                "     \"endTime\":\"2021-04-28 02:00:00\",\n" +
                "     \"track\":{1000:1,1001:1}" +
                "     }";
        RevivalInfo revivalInfo = JSON.parseObject(json, RevivalInfo.class);
        return Optional.ofNullable(revivalInfo);
    }

    //是否在复活赛
    public static Optional<Boolean> ifRevival(RevivalInfo dataInfo, LocalDateTime now){
        if(Optional.ofNullable(dataInfo).isPresent()){
            boolean result = (now.isAfter(dataInfo.getStartTime()) || now.isEqual(dataInfo.getStartTime()))
                    &&
                    (now.isBefore(dataInfo.getEndTime()) ||now.isEqual(dataInfo.getEndTime()) );
            log.info("DanceKingRankUtil.ifRevival.result:{}", result);
            return Optional.ofNullable(result);
        }
        log.error("DanceKingRankUtil.ifRevival.dataInfo is :{}",dataInfo);
        return Optional.empty();
    }
    //
    public static Optional<NewcomerAward> getNewcomerAward(){
        NewcomerAward newcomerAward = NewcomerAward.builder()
                .fromStage(1).currentStage(2).toStage(3)
                .build();
        return Optional.ofNullable(newcomerAward);
    }
    //是否展示赛果
    public static Optional<Boolean> ifShowResults(ResultsInfo dataInfo, LocalDateTime now){
        if(Optional.ofNullable(dataInfo).isPresent()){
                boolean result = (now.isAfter(dataInfo.getStartTime()) || now.isEqual(dataInfo.getStartTime()))
                        &&
                        (now.isBefore(dataInfo.getEndTime()) ||now.isEqual(dataInfo.getEndTime()) );
                log.info("DanceKingRankUtil.ifShowResults.result:{}", result);
                return Optional.ofNullable(result);
        }
        log.error("DanceKingRankUtil.ifShowResults.dataInfo is :{}",dataInfo);
        return Optional.empty();
    }

    public static Track checkStarTrack(Optional<Stage> optionalStage,String starid){
        if (!optionalStage.isPresent()){
            return null;
        }
        Stage stage = optionalStage.get();
        if (stage.isIfFinal()){
            return Track.FINALS;
        }else {
            //判断大小赛道
//            Track.BIG;
//            Track.LITTLE;
            return Track.LITTLE;
        }
    }


    /**
     *
     * @param currentStage
     * @param stageMap
     * @return
     */
    public static Optional<Stage> getPreStage(Optional<Stage> currentStage, Map<Integer, Stage> stageMap){
        if (currentStage.isPresent()){
            Stage stage = currentStage.get();
            Integer preStageNo = stage.getPreStageNo();
            if(null!=preStageNo && preStageNo>0 && !CollectionUtils.isEmpty(stageMap)){
                Stage preStage = stageMap.get(preStageNo);
                return Optional.ofNullable(preStage);
            }
        }
        return Optional.empty();
    }

    /**
     * 获取最后一个赛段
     * @param stages
     * @return
     */
    public static Optional<Stage> getFinalStage(Map<Integer, Stage> stages) {
        return stages.keySet().stream()
                .max(Integer::compareTo)
                .flatMap(val -> Optional.ofNullable(stages.get(val)));
    }


//    public static boolean pushDiscuss(String text) {
//        String env  =  VALUE_ENV_STAGE.equals(MomoEnv.env()) ? "[测试环境]：" : VALUE_ENV_SANDBOX.equals(MomoEnv.env()) ? "[沙盒环境]:" : "[生产环境]:";
//        String from = "10013";
//        String to = "24218662";
//        return PushTextUtil.pushDiscuss(from, to, env + text, AppConstant.IM_PROXY_BUSILINE.getCode()
//                , AppConstant.IM_PROXY_BUSISCENE.getCode(), AppConstant.IM_PROXY_APPSECRET.getCode());
//
//    }

    public static void main(String[] args) {
        Optional<Stage> finalStage = getFinalStage(new HashMap<>());
        System.out.println(finalStage.isPresent());

    }


}
